/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.praticas.projetoacademia.dao;

import br.praticas.projetoacademia.util.HibernateUtil;
import br.praticas.projetoacademia.models.Plano;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author KAMILO
 */
public class PlanoDao {

private Session sessao;

    public void add(Plano plano) {
         sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(plano);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public void update(Plano plano) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(plano);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public void remove(Plano plano) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(plano);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public List<Plano> listarPlanos() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        List<Plano> lista = sessao.createCriteria(Plano.class).list();
        sessao.close();
        return lista;
    }

    public Plano RetornaPlano(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Plano plano = (Plano) sessao.get(Plano.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return plano;
    }

    public Plano buscarPlano(int index) {
        List<Plano> lista = new ArrayList<>();
        Plano planoRetorno = null;
        lista = listarPlanos();
        int cont = 0;
        for (Plano plano : lista) {
            if (cont == index) {
                planoRetorno = plano;
                break;
            }
        }
        return planoRetorno;
    }

    public Session getSessao() {
        return this.sessao;
    }

    public void addItem(String nome) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}
